<?php
    function get_weixinApi_content($apiurl,$access_token=''){
        if (!empty($access_token)){
            $apiurl =str_replace("ACCESS_TOKEN",$access_token,$apiurl);
        }
        $tempData =json_decode(file_get_contents($apiurl),true);
        return $tempData;
    }

function post_weixinApi($apiUrl,$post_data,$access_token){
    if (!empty($access_token)){
        $apiUrl =str_replace("ACCESS_TOKEN",$access_token,$apiUrl);
    }
//    $post_data ="body=$post_data";
//    $postdata = http_build_query($post_data);
    $options = array(
        'http' => array(
            'method' => 'POST',
            'header' => 'Content-type:application/x-www-form-urlencoded',
            'content' => $post_data,
            'timeout' => 15 * 60 // 超时时间（单位:s）
        )
    );
    $context = stream_context_create($options);
    $result = file_get_contents($apiUrl, false, $context);

    return $result;
}


function post_weixinApiByCurl($apiUrl, $post_string,$access_token) {
    if (!empty($access_token)){
        $apiUrl =str_replace("ACCESS_TOKEN",$access_token,$apiUrl);
    }

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_POSTFIELDS, 'mypost=' . $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//    curl_setopt($ch, CURLOPT_USERAGENT, "qianyunlai.com's CURL Example beta");
    $data = curl_exec($ch);
    curl_close($ch);

    return $data;
}


function createMenu($apiUrl, $post_string,$access_token){
    if (!empty($access_token)){
        $apiUrl =str_replace("ACCESS_TOKEN",$access_token,$apiUrl);
    }
    echo $apiUrl;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $apiUrl);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_string);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $tmpInfo = curl_exec($ch);
    if (curl_errno($ch)) {
        return curl_error($ch);
    }

    curl_close($ch);
    return $tmpInfo;

}